package com.example.blog.modules.article.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.article.dto.ArticleDTO;
import com.example.blog.modules.article.dto.ArticleQuery;
import com.example.blog.modules.article.dto.ArticleRequest;
import com.example.blog.modules.article.dto.ArticleResponse;
import com.example.blog.modules.article.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
@RequiredArgsConstructor
@Tag(name = "文章管理", description = "文章的CRUD操作")
public class ArticleController {

    private final ArticleService articleService;

    @Operation(summary = "创建文章")
    @PostMapping
    @HasPermission("article:create")
    public ApiResponse<ArticleDTO> createArticle(@Valid @RequestBody ArticleRequest request) {
        return ApiResponse.success(articleService.createArticle(request));
    }

    @Operation(summary = "更新文章")
    @PutMapping("/{id}")
    @HasPermission("article:update")
    public ApiResponse<ArticleDTO> updateArticle(@PathVariable Long id, @Valid @RequestBody ArticleRequest request) {
        return ApiResponse.success(articleService.updateArticle(id, request));
    }

    @Operation(summary = "删除文章")
    @DeleteMapping("/{id}")
    @HasPermission("article:delete")
    public ApiResponse<Void> deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return ApiResponse.success();
    }

    @Operation(summary = "获取文章详情")
    @GetMapping("/{id}")
    @HasPermission("article:read")
    public ApiResponse<ArticleDTO> getArticle(@PathVariable Long id) {
        return ApiResponse.success(articleService.getArticle(id));
    }

    @Operation(summary = "获取文章列表")
    @GetMapping
    @HasPermission("article:read")
    public ApiResponse<Page<ArticleDTO>> getArticles(ArticleQuery query, Pageable pageable) {
        return ApiResponse.success(articleService.getArticles(query, pageable));
    }

    @Operation(summary = "增加文章浏览量")
    @PostMapping("/{id}/view")
    @HasPermission("article:read")
    public ApiResponse<Void> incrementViewCount(@PathVariable Long id) {
        articleService.incrementViewCount(id);
        return ApiResponse.success();
    }

    @Operation(summary = "点赞文章")
    @PostMapping("/{id}/like")
    @HasPermission("article:like")
    public ApiResponse<Void> likeArticle(@PathVariable Long id) {
        articleService.incrementLikeCount(id);
        return ApiResponse.success();
    }

    @Operation(summary = "取消点赞文章")
    @DeleteMapping("/{id}/like")
    @HasPermission("article:like")
    public ApiResponse<Void> unlikeArticle(@PathVariable Long id) {
        articleService.decrementLikeCount(id);
        return ApiResponse.success();
    }

    @Operation(summary = "发布文章")
    @PostMapping("/{id}/publish")
    @HasPermission("article:publish")
    public ApiResponse<Void> publishArticle(@PathVariable Long id) {
        articleService.publishArticle(id);
        return ApiResponse.success();
    }

    @Operation(summary = "取消发布文章")
    @PostMapping("/{id}/unpublish")
    @HasPermission("article:publish")
    public ApiResponse<Void> unpublishArticle(@PathVariable Long id) {
        articleService.unpublishArticle(id);
        return ApiResponse.success();
    }

    @Operation(summary = "置顶文章")
    @PostMapping("/{id}/top")
    @HasPermission("article:top")
    public ApiResponse<Void> topArticle(@PathVariable Long id) {
        articleService.topArticle(id);
        return ApiResponse.success();
    }

    @Operation(summary = "取消置顶文章")
    @PostMapping("/{id}/untop")
    @HasPermission("article:top")
    public ApiResponse<Void> untopArticle(@PathVariable Long id) {
        articleService.untopArticle(id);
        return ApiResponse.success();
    }

    @Operation(summary = "开启文章评论")
    @PostMapping("/{id}/enable-comment")
    @HasPermission("article:update")
    public ApiResponse<Void> enableComment(@PathVariable Long id) {
        articleService.enableComment(id);
        return ApiResponse.success();
    }

    @Operation(summary = "关闭文章评论")
    @PostMapping("/{id}/disable-comment")
    @HasPermission("article:update")
    public ApiResponse<Void> disableComment(@PathVariable Long id) {
        articleService.disableComment(id);
        return ApiResponse.success();
    }
} 